//
//  XSquareBaseCell.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/19.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import UIKit
/// 微博 Cell 的协议
/// 如果需要设置可选协议方法
/// - 需要遵守 NSObjectProtocol 协议
/// - 协议需要是 @objc 的
/// - 方法需要 @objc optional
@objc protocol XSquareBaseCellDelegate: NSObjectProtocol {
    
    /// 微博 Cell 选中 URL 字符串
    @objc optional func statusCellDidSelectedURLString(urlString: String)
    
    @objc optional func statusCellDidSelectedLike(indexPath:IndexPath,model:XSquareNews)
}
/// 鸽友圈cell基类
///子类需要实现 addViewToCell()在这个方法加添加所需的view
///setupViewInCell在这个方法加添加自动布局代码
class XSquareBaseCell: UITableViewCell {
    
    /// cell的索引
    var indexPath: IndexPath?
    
    /// 代理属性
    weak var delegate: XSquareBaseCellDelegate?
    
    //公共头部
    lazy var topView:XSquareCellTopView = XSquareCellTopView()
    
    // 正文
    lazy var statusLabel: FFLabel = FFLabel.cz_label(withText: "我会放弃吗，搞笑，哈哈哈哈哈哈只带你事合我刚看完个哪个县只带你带你咀哈只带你一234928u4h2i34hjkk", fontSize: 15, color: UIColor.darkGray)//FFLabel
    
    lazy var retweetBack: UIButton = UIButton()
    //配图
    lazy var picView:XSquarePicsView = XSquarePicsView()
    //底部工具栏
    lazy var bottomView: XSquareCellBottomView = XSquareCellBottomView()
    
    //配图
    //转发微博的正文
    lazy var retweetedLabel:FFLabel = FFLabel.cz_label(withText: "我会放弃吗，搞笑，哈哈哈哈哈哈只带你事合我刚看完个哪个县只带你带你咀哈只带你一234928u4h2i34hjkk", fontSize: 14, color: UIColor.darkGray) //FFLabel?
    
    
    var viewModel:XSquareNewsViewModel?{
        didSet{
            topView.viewModel = viewModel
            statusLabel.attributedText = viewModel?.statusAttrText
            retweetedLabel.attributedText = viewModel?.retweetedAttrText
            bottomView.viewModel = viewModel
            picView.viewModel = viewModel
            
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    /// 添加
    func addViewToCell(){
    }
    // 原创微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
    // 被转发微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 间距(12)+间距(12)+转发文本高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
    func setupViewInCell(){
        
        
    }
    
    
}
// MARK: - UI搭建
fileprivate extension XSquareBaseCell{
    
    /// UI搭建
    func setupUI(){
        selectionStyle = .none
        //离屏渲染
        self.layer.drawsAsynchronously = true
        //栅格化
        self.layer.shouldRasterize = true
        //使用栅格化必须指定分辨率
        self.layer.rasterizationScale = UIScreen.main.scale
        
        // 设置微博文本代理
        statusLabel.delegate = self
        retweetedLabel.delegate = self
        bottomView.delegate = self
        //        backgroundColor = UIColor.cz_random()
        addViewToCell()
        setupViewInCell()
    }
    
    
}
extension XSquareBaseCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        // 判断是否是 URL
        if !text.hasPrefix("http://") {
            return
        }
        
        // 插入 ? 表示如果代理没有实现协议方法，就什么都不做
        // 如果使用 !，代理没有实现协议方法，仍然强行执行，会崩溃！
        delegate?.statusCellDidSelectedURLString?(urlString: text)
    }
}
extension XSquareBaseCell:XSquareCellBottomViewDelegate{
    func bottomDidSelectedLike() {
//        print("点赞了")
        guard let news = viewModel?.squareNews,
        let indexPath = indexPath
            else{
                return
        }
        
        delegate?.statusCellDidSelectedLike?(indexPath: indexPath, model: news)
    }
    
}

